The comma operator takes two expressions, executes them from left to right, and returns the result of the second one. The use of this operator is
generally detrimental to the readability and reliability of code, and the same effect can be achieved by other means.
i = a += 2, a + b; // Noncompliant: What's the value of i ?
Writing each expression on its own line will improve readability and might fix misunderstandings.
a += 2;
i = a + b; // We probably expected to assign the result of the addition to i, although the previous code wasn't doing it.
Exceptions
The comma operator is tolerated:
- In initializations and increment expressions of
for
loops.
for (i = 0, j = 5; i < 6; i++, j++) { ... }
- If the expression sequence is explicitly wrapped in parentheses.
i = (a += 2, a + b); // Compliant by exception